# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# GNU Makefile based on shared rules provided by the Native Client SDK.
# See README.Makefiles for more details.

VALID_TOOLCHAINS := newlib
#glibc newlib pnacl

CONFIG ?= Debug

# Redefine this to your libchewing source home.
# Also create "data" symlink folder to point into the data in libchewing.
LIBCHEWING_HOME ?= libchewing
LIBCHEWING_URL ?= https://github.com/chewing/libchewing.git
LIBCHEWING_BRANCH ?=

NACL_SDK_ROOT ?= $(abspath $(CURDIR)/../../../../nacl_sdk)
include $(NACL_SDK_ROOT)/tools/common.mk
CFLAGS += -DHAVE_CONFIG_H -I. -I$(LIBCHEWING_HOME)/src
CFLAGS += -I$(LIBCHEWING_HOME)/src/porting_layer/include
CFLAGS += -I$(LIBCHEWING_HOME)/include -I$(LIBCHEWING_HOME)/include/internal

TARGET = chewing
LIBS = $(DEPS) ppapi_cpp ppapi nacl_io pthread jsoncpp

SOURCES = \
	$(LIBCHEWING_HOME)/src/chewingio.c \
	$(LIBCHEWING_HOME)/src/chewingutil.c \
	$(LIBCHEWING_HOME)/src/choice.c \
	$(LIBCHEWING_HOME)/src/dict.c \
	$(LIBCHEWING_HOME)/src/mod_aux.c \
	$(LIBCHEWING_HOME)/src/pinyin.c \
	$(LIBCHEWING_HOME)/src/tree.c \
	$(LIBCHEWING_HOME)/src/zuin.c \
	$(LIBCHEWING_HOME)/src/common/chewing-utf8-util.c \
	$(LIBCHEWING_HOME)/src/common/key2pho.c \
	$(LIBCHEWING_HOME)/src/porting_layer/src/plat_mmap_posix.c \
	$(LIBCHEWING_HOME)/src/porting_layer/src/plat_path.c \
	nacl.cc

# User data (hash)
SOURCES += $(LIBCHEWING_HOME)/src/userphrase.c
SOURCES += $(LIBCHEWING_HOME)/src/hash.c
SOURCES += $(LIBCHEWING_HOME)/src/userphrase-hash.c

ifneq ($(CONFIG),Release)
CFLAGS+=-DDEBUG
else
# Unfortunately chewing is not ready for -Wall.
# CFLAGS+=-Wall
endif

$(LIBCHEWING_HOME):
	git clone $(LIBCHEWING_BRANCH) $(LIBCHEWING_URL)
	# We need to build some native files.
	cd $(LIBCHEWING_HOME) && ./autogen.sh && ./configure && make

$(LIBCHEWING_HOME)/src/chewingio.c:	$(LIBCHEWING_HOME)

# Build rules generated by macros from common.mk:

$(foreach dep,$(DEPS),$(eval $(call DEPEND_RULE,$(dep))))
$(foreach src,$(SOURCES),$(eval $(call COMPILE_RULE,$(src),$(CFLAGS))))

ifeq ($(CONFIG),Release)
$(eval $(call LINK_RULE,$(TARGET)_unstripped,$(SOURCES),$(LIBS),$(DEPS)))
$(eval $(call STRIP_RULE,$(TARGET),$(TARGET)_unstripped))
else
$(eval $(call LINK_RULE,$(TARGET),$(SOURCES),$(LIBS),$(DEPS)))
endif

$(eval $(call NMF_RULE,$(TARGET),))
